<?php
if (isset($_POST['modifier'])) 
{
	$req = $bdd->prepare('UPDATE categories_recettes SET nom = :nom, description = :description WHERE id=:id');
	$req->execute(array(
		'nom' => stripslashes($_POST['nom']),
		'description' => stripslashes($_POST['description']),
		'id' => $_POST['id']
	));
	header('Location: index.php?page=admin_categories_recettes'); 
}
elseif (isset($_POST['effacer'])) 
{
	$req = $bdd->prepare('DELETE FROM categories_recettes WHERE id=:id');
	$req->execute(array('id' => $_POST['id']));
	header('Location: index.php?page=admin_categories_recettes'); 
}
elseif (isset($_POST['ajouter'])) 
{
	$req = $bdd->prepare('INSERT INTO categories_recettes (nom, description) VALUES (:nom, :description)');
	$req->execute(array(
		'nom' => stripslashes($_POST['nom']),
		'description' => stripslashes($_POST['description'])
	));
	header('Location: index.php?page=admin_categories_recettes'); 
}
?>
<h1 id="titre">Gestion des catégories de recettes</h1>
<table border="0" width="100%">
	<?php
	$req = $bdd->query('SELECT * FROM categories_recettes ORDER BY nom');
	while ($donnees = $req->fetch())
	{
		?>
		<form method="post" action="index.php?page=admin_categories_recettes">
			<tr>
			<td width="40%"><input type="text" name="nom" value="<?php echo stripslashes($donnees['nom']); ?>" maxlength="250" size="20"></td>
			<td width="20%"><input type="text" name="description" value="<?php echo stripslashes($donnees['description']); ?>" maxlength="250" size="30">
			<input type="hidden" name="id" value="<?php echo $donnees['id']; ?>" /></td>
			<td width="40%"><input type="submit" value="Modifier" name="modifier" class="bouton" />
			<input type="submit" value="Effacer" name="effacer" class="bouton" /></td>
			</tr>
		</form>
		<?php
	}
	$req->closeCursor();
	?>
	<tr><td colspan="3"><hr /></td></tr>
	<form method="post" action="index.php?page=admin_categories_recettes">
		<tr>
		<td><input type="text" name="nom" value="" maxlength="250" size="20"></td>
		<td><input type="text" name="description" maxlength="250" size="30"></td>
		<td width="25%"><input type="submit" value="Ajouter cette catégorie" name="ajouter" class="bouton" /></td>
		</tr>
	</form>
</table>